package com.example.blog.module.controller;


import com.example.blog.module.common.aop.LogAnnotation;
import com.example.blog.module.common.cache.Cache;
import com.example.blog.module.component.MyElasticsearchRepository;
import com.example.blog.module.entity.PageParams;
import com.example.blog.module.entity.VO.*;
import com.example.blog.module.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjx
 * @since 2022-11-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
public class ArticleController {

    private final ArticleService articleService;

    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    @LogAnnotation(module = "文章",operation = "获取文章列表")
    public Result articles(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 最热文章--走缓存
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticles(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 个人文章归档
     * @return
     */
    @PostMapping("personArchives")
    public Result personArchives(){
        return articleService.personArchives();
    }


    /**
     * 首页 最新文章
     * @return
     */
    @PostMapping("new")
    public Result newArticles(){
        int limit = 5;
        return articleService.newArticles(limit);
    }

    /**
     * 查看某一篇文章
     * @param articleId
     * @return
     */
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    /**
     * 发布文章
     * @param articleParam
     * @return
     */
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @PostMapping("update")
    public Result update(@RequestBody ArticleParam articleParam){
        return articleService.updateById(articleParam);
    }
}

